package com.fare.taluo.base.struts2;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.inject.Inject;

@ParentPackage("struts-convention-fdc")
public abstract class BaseConventionStrutsAction extends ActionSupport {
	/**
	 * log4j日志记录
	 */
	protected static Logger logger = Logger.getLogger(BaseConventionStrutsAction.class);
	/** 
	 * @Fields serialVersionUID 
	 */ 
	private static final long serialVersionUID = -2352933873997140918L;

	@Inject("struts.multipart.saveDir")   
	protected String multipartSaveDir; 
	
	@Inject("struts.convention.result.path")   
	protected String resultPath; 
	
	@Inject("struts.action.extension")   
	protected String actionExtension;
	
	protected HttpServletRequest request;
	
	protected HttpServletResponse response;

	public BaseConventionStrutsAction(){
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
	}
	public String getMultipartSaveDir() {
		return multipartSaveDir;
	}

	public void setMultipartSaveDir(String multipartSaveDir) {
		this.multipartSaveDir = multipartSaveDir;
	}

	public String getResultPath() {
		return resultPath;
	}

	public void setResultPath(String resultPath) {
		this.resultPath = resultPath;
	}


	public String getActionExtension() {
		return actionExtension;
	}

	public void setActionExtension(String actionExtension) {
		this.actionExtension = actionExtension;
	}
	
	/**
	 * 执行数据回写至页面 synchronized保证线程安全
	 * @param jsonString
	 * @return
	 */
	public static synchronized void toJSONOut(String str){
		try {
			javax.servlet.http.HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/json;charset=UTF-8");
			java.io.PrintWriter out = response.getWriter();
			out.print(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 执行数据回写至页面缓存 synchronized保证线程安全
	 * 
	 * @param jsonString
	 * @return
	 */
	public static synchronized void toJSONHtmlOut(String str){
		try {
			javax.servlet.http.HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=UTF-8");
			java.io.PrintWriter out = response.getWriter();
			out.print(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
